<?php

namespace App\Http\Controllers;


use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


    public function message($message, $url = '', $type = 'success')
    {
        if (request()->ajax()) {
            return response()->json([
                'code' => $type,
                'msg' => $message
            ]);
        }

        if (!$url) {
            $url = back();
        }

        return redirect($url)->with([
            'msg' => $message,
            'type' => $type
        ]);
    }

    public function success($message = 'success', $url = '')
    {
        return $this->message($message, $url, $type = 'success');
    }


    public function error($message = 'error', $url = '')
    {
        return $this->message($message, $url, $type = 'error');
    }

    public function jsonSuccess($msg = 'success', $data = [])
    {
        $res = [
            'code' => 'success',
            'msg' => $msg
        ];

        if ($data) {
            $res['data'] = $data;
        }
        return response()->json($res);
    }

    public function jsonError($msg = 'error')
    {
        $res = [
            'code' => 'success',
            'msg' => $msg
        ];
        return response()->json($res);
    }

}
